package again.newlife.xml;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

public class XmlTest {
    // 任务：解析 xml 文件
    public static void main(String[] args) throws Exception {
        //1. 创建解析对象
        SAXReader saxReader = new SAXReader();
        // 2. 读取games.xml文件获得一个document对象
        Document document = saxReader.read(new FileReader("day14-code/src/again/newlife/file/Game.xml"));
        // 3. 获取根节点对象
        Element rootElement = document.getRootElement();
        //4. 输出
        System.out.println("根标签名："+rootElement.getName());
        //5. 获取子标签节点
        List<Element> elements = rootElement.elements();// 获取所有子标签
        for (Element element : elements) {
            String name = element.getName();// 获取子标签名称
            System.out.println("\t子标签名称："+name);
            System.out.println("\t子标签的属性名称："+element.attributeValue("id"));

            //获取孙子标签
            Element name1 = element.element("name");
            System.out.println("\t\t孙子标签名称："+name1.getName());
            System.out.println("\t\t孙子标签内容："+name1.getText());
            Element company = element.element("company");
            System.out.println("\t\t孙子标签名称："+company.getName());
            System.out.println("\t\t孙子标签内容："+company.getText());
        }

    }
}
